<template>
  <div class="graph-con">
    <div class="loading" v-if="loading"><i class="el-icon-loading"></i></div>
    <div v-if="!isData" class="is-data">{{ text }}</div>
    <div class="canvas">
      <div v-for="(item, index) in comboData" :key="index">
        <ComboList
          v-if="item.child.length > 0 && !item.collapsed"
          :data="item"
          @clickNode="clickNode"
        />
        <NodeList
          v-else
          :nodeData="item"
          @extendNode="extendNode"
          @clickNode="clickNode"
        />
      </div>
    </div>
  </div>
</template>

<script>
import ComboList from "./ComboList.vue";
import NodeList from "./NodeList.vue";
export default {
  components: {
    ComboList,
    NodeList,
  },
  data() {
    return {
      loading: false,
      text: "暂无数据。。。",
      isData: true,
      comboData: [],
    };
  },
  methods: {
    // 获取全部节点数据
    getAllData(code) {
      this.loading = true;
      this.isData = true;
      this.$get(this.$Url.industrial.allNodeList, {
        parent: code,
      }).then((res) => {
        this.loading = false;
        if (res.data.length > 0) {
          //方案三：Combo 固定定位布局
          this.comboData = this.changeComboData(res.data, 0);
        } else {
          this.isData = false;
        }
      });
    },
    //方案三：Combo 固定定位布局
    changeComboData(data, num) {
      const arr = [];
      data.map((item) => {
        if (arr.find((val) => val.code === item.code)) return;
        arr.push({
          ...item,
          collapsed: num > 1 ? true : false,
          child: item.child ? this.changeComboData(item.child, num + 1) : [],
        });
      });
      return arr;
    },
    clickNode(code) {
      this.$emit("changeCode", code);
    },
    extendNode(data) {
      data.collapsed = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.graph-con {
  flex: 1;
  height: 100%;
  position: relative;
  .is-data {
    height: 100%;
    width: 100%;
    font-size: 20px;
    text-align: center;
    line-height: 600px;
    position: absolute;
    top: 0;
    left: 0;
  }
  .loading {
    width: 100%;
    height: 100%;
    background: #fff;
    opacity: 0.8;
    position: absolute;
    top: 0;
    left: 0;
    font-size: 60px;
    text-align: center;
    color: #524cab;
    line-height: 600px;
    font-weight: 700;
    z-index: 999;
  }
  .canvas {
    height: 100%;
    display: flex;
    justify-content: space-between;
    padding: 50px 0 50px 20px;
    > div {
      margin-right: 50px;
    }
  }
}
</style>